package com.example.role.service.serviceimpl;



import com.example.role.auth.CookieUtil;
import com.example.role.auth.JWTUtil;
import com.example.role.mapper.UserMapper;
import com.example.role.pojo.User;
import com.example.role.service.IUserService;

import com.example.role.util.Const;
import com.example.role.util.ServerResponse;
import com.example.role.util.TableServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Service
@Transactional

public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    //登录，生成token存入cookie
    @Override
    public ServerResponse login(User user, HttpServletResponse response) {
        User currentUser = userMapper.login(user);
        if (currentUser != null) {
            //生成token
            String token = JWTUtil.generateToken(currentUser, 30, Const.PRI_RSA_PATH);
            //向浏览器响应cookie
            CookieUtil.resposeCookie(token, 1800, response);
            //登录成功
            return ServerResponse.createSuccess();
        } else {
            return ServerResponse.createError("账号或密码错误");
        }
    }
        //判读账号密码一直性
    @Override
    public ServerResponse forgetPassword(User user) {
        Integer result = userMapper.forgetPassword(user);
        if (result > 0) {
            return ServerResponse.createSuccess();
        } else {
            return ServerResponse.createError("账号和身份证不匹配");
        }
    }
        //修改密码
    @Override
    public ServerResponse updatePassword(User user) {
        Integer result = userMapper.updatePassword(user);
        if (result > 0) {
            return ServerResponse.createSuccess("修改成功，请重新登录");
        } else {
            return ServerResponse.createError("修改失败");
        }
    }
//查询用户信息
    @Override
    public TableServerResponse getUser(Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        //查询
        List<User> list = userMapper.getUser();
        //查询条数
        PageInfo objectPageInfo = new PageInfo(list);
        long total = objectPageInfo.getTotal();
        return TableServerResponse.createSuccess(0, list, total);
    }
//修改用户角色
    @Override
    public ServerResponse updateRole(User user) {
        Integer result = userMapper.updateRole(user);
        if (result > 0) {
            return ServerResponse.createSuccess();
        }else{
            return ServerResponse.createError();
        }
    }
//判断用户是否登录储存cookie
    @Override

    public ServerResponse getUserinfo(String token) {
        //判断utoken是否为空
        if(token != null){
            User user = JWTUtil.parserToken(token, Const.PUB_RSA_PATH);
            if (user != null) {
                //解析成功
                return ServerResponse.createSuccess(user);
            }else{
                return ServerResponse.createError("请重新登录");
            }
        }else{
            return ServerResponse.createError("用户未登录,无法获取当前用户信息");
        }
    }
//删除用户
    @Override
    public ServerResponse selectUserById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(user)){
            return ServerResponse.createError("出现未知错误");
        }else{
            return ServerResponse.createSuccess(user);
        }
    }
//修改用户信息
    @Transactional(readOnly = true)
    @Override
    public ServerResponse updateUser(String token, User user) {
        User tokenUser = JWTUtil.parserToken(token, Const.PUB_RSA_PATH);
        user.setUserId(tokenUser.getUserId());
        int result = userMapper.updateByPrimaryKeySelective(user);
        if(result > 0 ){
            return ServerResponse.createSuccess("修改成功");
        }else{
            return ServerResponse.createError("修改失败");
        }
    }
//增加用户
    @Override
    public ServerResponse addUser(User user) {
        //判断账号是否存在
        int row;
        row = userMapper.selectByAccount(user.getUserAccount());
        if(row > 0){
            return ServerResponse.createError("该账户已存在，请重新生成");
        }
        int result = userMapper.insertSelective(user);
        if(result > 0 ){
            return ServerResponse.createSuccess();
        }else{
            return ServerResponse.createError();
        }
    }
//更具id删除列表
    @Override
    public ServerResponse deleteUser(String ids) {
        String[] array = ids.split(",");
        Integer result = userMapper.deleteUser(array);
        if(result > 0){
            return ServerResponse.createSuccess();
        }else{
            return ServerResponse.createError();
        }
    }
}
